/**
 * 
 */
package com.novartis.nims.trialsubmitter;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author snydejo1
 *
 * contains a map of arbitrary keys to relative URIs which represent references to image files
 * also contains a map of same keys to SubmissionKeys. each SubmissionKey contains a set of clinical
 * trial identifiers.
 */
class Trial {
	private Map<Integer, URI> mImageReference;
	private Map<Integer, SubmissionKey> mImageIdentifier;
	
	private URI mBaseLocation = null;;
	
	Trial() {
		mImageReference = new HashMap<Integer, URI>();
		mImageIdentifier = new HashMap<Integer, SubmissionKey>();
	}
	
	public Set<Integer> keySet() {
		return mImageReference.keySet();
	}
	
	public boolean isEmpty() {
		return mImageReference.isEmpty();
	}
	
	public int size() {
		return mImageReference.size();
	}
	
	/**
	 * add an image to the trial. this consists of storing the image location and the trial identifiers.
	 * the image location and trial identifiers can be retrieved using an externally generated image key
	 * which must be unique within the trial. if this trial already contains an image associated with the
	 * image key provided, the existing image will remain and this method will return null.
	 * 
	 * @param imageKey - a unique identifier used to key the image to add
	 * @param location - a URI referencing the image to add
	 * @param hash - a String containing the SHA1 hash of the image to add
	 * @param trialKey - an object containing the trial identifiers associated with the image to add 
	 * @return {@code true} if the image was added. {@code false} if the imageKey was already present
	 * and the image wasn't added.
	 */	
	boolean insertImage(Integer imageKey, URI location, SubmissionKey trialKey) {
		URI oldValue = null;
		oldValue = mImageReference.put(imageKey, location);
		if (oldValue != null) {
			// restore pre-existing location
			mImageReference.put(imageKey, oldValue);
		} else {
			insertTrialKey(imageKey, trialKey);
		}
		
		return oldValue == null;
	}
	
	private void insertTrialKey(Integer imageKey, SubmissionKey trialKey) {
		SubmissionKey oldValue = null;
		oldValue = mImageIdentifier.put(imageKey, trialKey);
		if (oldValue != null) {
			// restore pre-existing trial key
			mImageIdentifier.put(imageKey, oldValue);
		}
	}

	public void removeImage(Integer imageKey) {
		mImageReference.remove(imageKey);
		mImageIdentifier.remove(imageKey);
	}

	//return previous image reference. don't insert, and return null if image was not already present.
	public URI updateImageReference(Integer imageKey, URI reference) {
		return (mImageReference.get(imageKey) == null) ? null : mImageReference.put(imageKey, reference);
	}

	//return previous image identifier. don't insert, and return null if image was not already present.
	public SubmissionKey updateImageIdentifier(Integer imageKey, SubmissionKey trialKey) {
		return (mImageIdentifier.get(imageKey) == null) ? null : mImageIdentifier.put(imageKey, trialKey);
	}

	//returns null if there is no image trial associated with the provided image key
	public URI getImageReference(Integer imageKey) {
		return mImageReference.get(imageKey);
	}

	//returns null if there is no image trial associated with the provided image key
	public SubmissionKey getImageIdentifier(Integer imageKey) {
		return mImageIdentifier.get(imageKey);
	}

	public void setBaseLocation(URI location) {
		mBaseLocation = location;
	}

	public URI getBaseLocation() {
		return mBaseLocation;
	}
}
